package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageDetailBatchActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageProductionOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageProductionOverviewBean;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ProductionOutStorageItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<ItemListBean> mProductionOutStorageItemList;
    private LayoutInflater inflater;
    private OutStorageProductionOverviewActivity.OutStorageProductionOverviewListener mOutStorageProductionListener;
    private String mMaterielCode, mPickingNumber, mDepartment, mApplicant, mCreateTime;

    public ProductionOutStorageItemListAdapter(Context context, OutStorageProductionOverviewBean.BodyBean data, List<ItemListBean> outStorageProductionItemList, String materielCode, OutStorageProductionOverviewActivity.OutStorageProductionOverviewListener outStorageProductionOverviewListener) {
        mContext = context;
        mPickingNumber = data.getRefer_receipt_code();
        mDepartment = data.getApply_depart_name();
        mApplicant = data.getApply_user_name();
        mCreateTime = data.getApply_time();
        mProductionOutStorageItemList = outStorageProductionItemList;
        mOutStorageProductionListener = outStorageProductionOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mProductionOutStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mProductionOutStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_out_storage_picking_item_list_first_item, null);
            TextView department = (TextView) convertView.findViewById(R.id.department);
            TextView applicant = (TextView) convertView.findViewById(R.id.applicant);
            TextView createTime = (TextView) convertView.findViewById(R.id.createTime);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            department.setText(mDepartment);
            applicant.setText(mApplicant);
            createTime.setText(mCreateTime);
            materielCode.setText(mMaterielCode);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStorageProductionListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            ItemListBean ProductionOutstorageItem = mProductionOutStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_picking_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, ProductionOutstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.rid = (TextView) view.findViewById(R.id.rid);
        viewHolder.type = (TextView) view.findViewById(R.id.type);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ItemListBean productionOutStorageItem) {
        viewHolder.rid.setText(productionOutStorageItem.getRefer_receipt_rid());
        viewHolder.type.setText(productionOutStorageItem.getMat_store_type());
        viewHolder.materielCode.setText(productionOutStorageItem.getMat_code());
        viewHolder.materielDescription.setText(productionOutStorageItem.getMat_name());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(productionOutStorageItem.getActualSendNumber()) ? mContext.getResources().getString(R.string.activity_out_storage_sale_overview_real_hair)
                : (mContext.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number) + productionOutStorageItem.getActualSendNumber() + productionOutStorageItem.getUnit_name()));
        if (TextUtils.isEmpty(productionOutStorageItem.getActualSendNumber())) {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent;
                switch (productionOutStorageItem.getMat_store_type()) {
                    case OutStorageUtils.TYPE_BATCH:
                        //批次
                        intent = new Intent(context, OutStorageDetailBatchActivity.class);
                        intent.putExtra(OutStorageExtra.SERIAL, productionOutStorageItem.getRefer_receipt_rid());
                        intent.putExtra(OutStorageExtra.TYPE, OutStorageUtils.OUT_STORAGE_TYPE_PRODUCTION);
                        context.startActivity(intent);
                        break;
                    case OutStorageUtils.TYPE_SN:
                        //序列号

                        break;
                    case OutStorageUtils.TYPE_NONE:
                        //无
                        //与按批次一样

                        break;
                }
            }
        });
    }

    private class ViewHolder {
        private TextView materielCode, materielDescription, outStorageNumber, rid, type;

        private PercentRelativeLayout headLayout;
    }
}
